// Copyright (C) 2009 Jesse Jones
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using MmmDoc;
using Mono.Cecil;
using System.Collections.Generic;

namespace MmmDoc.Database
{
	internal sealed class FieldMember : Member
	{
		public FieldMember(Document doc, TypeDef type, FieldDefinition member) : base(type, member.ToString())
		{
			Key = member.Name;
			Access = (MemberAccess) (member.Attributes & FieldAttributes.FieldAccessMask);
			Modifiers = DoGetModifiers(member);
			Name = member.Name;
			
			Type = TypeName.Create(doc, member.FieldType);
			Value = member.Constant;
			
			var attrs = new List<string>();
			member.CustomAttributes.AddAttributes(attrs);
			if (member.IsNotSerialized)
				attrs.Add("[NonSerialized]");
			CustomAttributes = attrs.ToArray();
		}
		
		/// <summary>May be null.</summary>
		public TypeName Type {get; private set;}
		
		/// <summary>Set for constant fields.</summary>
		public object Value {get; private set;}
		
		internal override SortKey GetSortKey()
		{
			return new SortKey{Name = Name};
		}
		
		internal override string GetXmlName(TypeDef type)
		{
			var builder = new System.Text.StringBuilder();
			
			builder.Append("F:");
			
			builder.Append(type.GetXmlName());
			builder.Append('.');
			builder.Append(Name);
			
			return builder.ToString();
		}
		
		#region Private Methods
		private string DoGetModifiers(FieldDefinition member)
		{
			var builder = new System.Text.StringBuilder();
			
			FieldAttributes attrs = member.Attributes;
			switch (attrs & FieldAttributes.FieldAccessMask)
			{
				case FieldAttributes.Compilercontrolled:
					builder.Append("compiler ");
					break;
					
				case FieldAttributes.Private:
					builder.Append("private ");
					break;
				
				case FieldAttributes.FamANDAssem:
					builder.Append("protected&internal ");
					break;
				
				case FieldAttributes.Assembly:
					builder.Append("internal ");
					break;
				
				case FieldAttributes.Family:
					builder.Append("protected ");
					break;
				
				case FieldAttributes.FamORAssem:
					builder.Append("protected internal ");
					break;
				
				case FieldAttributes.Public:
					builder.Append("public ");
					break;
				
				default:
					Contract.Assert(false, "bad access: " + (attrs & FieldAttributes.FieldAccessMask));
					break;
			}
			
			if ((attrs & FieldAttributes.Static) == FieldAttributes.Static)
				builder.Append("static ");
			
			if ((attrs & FieldAttributes.InitOnly) == FieldAttributes.InitOnly)
				builder.Append("readonly ");
			
			if ((attrs & FieldAttributes.Literal) == FieldAttributes.Literal)
				builder.Append("const ");
				
			return builder.ToString();
		}
		#endregion
	}
}
